/*
 *  Copyright 2009-2011 Ricardo Engel
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.mookt.unit;

import com.google.common.base.Objects;
import com.google.common.base.Preconditions;

/**
 * Base unit is a fundamental unit that is defined arbitrarily and not by
 * combinations of other units.
 * 
 * <p>
 * Examples of base units are metre, second and gram.
 * </p>
 * 
 * <p>
 * This class is immutable and thread-safe.
 * </p>
 * 
 * @author Ricardo Engel
 */
public class BaseUnit implements Unit {

    private static final long serialVersionUID = 1L;

    private final String symbol;

    private final Dimension dimension;

    /**
     * Constructor, creating a new {@code BaseUnit} instance.
     * 
     * @param symbol the unit symbol, not null
     * @param dimesion the unit dimension, not null
     */
    private BaseUnit(final String symbol, final Dimension dimension) {
        Preconditions.checkNotNull(symbol, "Symbol cannot be null");
        this.symbol = symbol;
        this.dimension = dimension;
    }

    /**
     * Returns an unit instance with the specified {@code symbol} and
     * {@code dimension}.
     * 
     * @param symbol the unit symbol, not null
     * @param dimesion the unit dimension, not null
     */
    public static BaseUnit of(final String symbol, final Dimension dimension) {
        return new BaseUnit(symbol, dimension);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Unit#getSymbol()
     */
    public String getSymbol() {
        return symbol;
    }

    /**
     * Returns the dimension of this unit.
     * 
     * @see Dimension
     */
    public Dimension getDimension() {
        return dimension;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mookt.unit.Unit#compatibleWith(org.mookt.unit.Unit)
     */
    public boolean compatibleWith(final Unit unit) {
        if (unit == this) {
            return true;
        } else if (dimension == null) {
            return false;
        } else if (unit instanceof BaseUnit) {
            final BaseUnit that = (BaseUnit) unit;
            return dimension.equals(that.dimension);
        } else {
            return false;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format("%s[%s]", symbol,
                Objects.firstNonNull(dimension, ""));
    }

}